<?php

/**
 * @author Samrat Khan
 * @author Zorj
 * Sep 21, 2013 - 4:44:23 PM
 */

ob_start();
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'final_warehouse';
//$table = 'stockin';
$file = 'Pickup';

$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_error());
mysql_select_db($db) or die("Can not connect.");

$values = mysql_query("
            SELECT 
                serialno, productid, description, revisionstate, return_quantity, 
                (SELECT measurement FROM uom WHERE uom = uom.uom_id) AS UOM, siteid,
                (SELECT statusname FROM faultystatus WHERE faultiesstatus = faultystatus.status_id) AS Faulty_Type,
                rma, pickuprequestdate, physicallyreceivedate, manufacturingdate, 
                (SELECT warehousename FROM warehouses WHERE warehouseid = warehouses.warehouse_id) AS WarehouseName,
                (SELECT employeename FROM employee WHERE employeeid = employee.employee_id) AS EmployeeName,
                (SELECT engname FROM engineerinfo WHERE eng_id = faulties.receivedfrom LIMIT 1), docketdate, faulties_docketnumber, remarks
            FROM faulties WHERE faultiesstatus = '1' AND junkentry = '1'
                UNION
            SELECT 
                serialno, productid, description, revisionstate, in_quantity, 
                (SELECT measurement FROM uom WHERE uom = uom.uom_id) AS UOM,
                siteid, 
                (SELECT typename FROM shiptype WHERE shiptype = shiptype.shiptype_id) AS Faulty_Type,
                rma, pickuprequestdate, receiveddate, manufacturingdate, 
                (SELECT warehousename FROM warehouses WHERE warehouse_id = warehouses.warehouse_id LIMIT 1) AS WarehouseName, 
                (SELECT employeename FROM employee WHERE employee_id = employee.employee_id LIMIT 1) AS EmployeeName,
                (SELECT engname FROM engineerinfo WHERE eng_id = stockin.eng_id LIMIT 1), docketdate, docketnumber, remarks
             FROM stockin WHERE shiptype = '6' AND junkentry = '1'
             ");
$headings = array( 'Serial No', 'Product ID', 'Description', 'Revision State', 'Quantity', 'UOM', 'Site ID', 'Type', 'Customer Request Number', 
                   'Pickup Request Date','Physically Receive Date', 'Manufacturing Date','Warehouse Name', 'Bearer Name', 'Received From',
                   'Docket Date','Docket Number','Remarks');
$i = 0;
foreach ($headings as $heading) {
    $csv_output .= $heading . ", ";
    $i++;
}
$csv_output .= "\n";
while($rowr = mysql_fetch_row($values)) {
    for($j = 0; $j < $i; $j++) {
        $csv_output .= '"'.$rowr[$j] . '",';
    }
$csv_output .= "\n";
}
ob_end_clean();



$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
//header("Content-type: text/csv");
header('Content-Length: ' . strlen($csv_output) );
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>